home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DESDOS.ARJ / DES.C < prev    next >
C/C++ Source or Header  |  1992-05-15  |  14KB  |  521 lines

  1. /* Sofware DES functions
  2.  * written 12 Dec 1986 by Phil Karn, KA9Q; large sections adapted from
  3.  * the 1977 public-domain program by Jim Gillogly
  4.  */
  5. #include <mem.h>
  6.  
  7. #define    NULL    0
  8.  
  9. #ifdef    LITTLE_ENDIAN
  10. unsigned long byteswap();
  11. #endif
  12.  
  13. /* Tables defined in the Data Encryption Standard documents */
  14.  
  15. /* initial permutation IP */
  16. static char ip[] = {
  17.     58, 50, 42, 34, 26, 18, 10,  2,
  18.     60, 52, 44, 36, 28, 20, 12,  4,
  19.     62, 54, 46, 38, 30, 22, 14,  6,
  20.     64, 56, 48, 40, 32, 24, 16,  8,
  21.     57, 49, 41, 33, 25, 17,  9,  1,
  22.     59, 51, 43, 35, 27, 19, 11,  3,
  23.     61, 53, 45, 37, 29, 21, 13,  5,
  24.     63, 55, 47, 39, 31, 23, 15,  7
  25. };
  26.  
  27. /* final permutation IP^-1 */
  28. static char fp[] = {
  29.     40,  8, 48, 16, 56, 24, 64, 32,
  30.     39,  7, 47, 15, 55, 23, 63, 31,
  31.     38,  6, 46, 14, 54, 22, 62, 30,
  32.     37,  5, 45, 13, 53, 21, 61, 29,
  33.     36,  4, 44, 12, 52, 20, 60, 28,
  34.     35,  3, 43, 11, 51, 19, 59, 27,
  35.     34,  2, 42, 10, 50, 18, 58, 26,
  36.     33,  1, 41,  9, 49, 17, 57, 25
  37. };
  38.  
  39. /* expansion operation matrix
  40.  * This is for reference only; it is unused in the code
  41.  * as the f() function performs it implicitly for speed
  42.  */
  43. #ifdef notdef
  44. static char ei[] = {
  45.     32,  1,  2,  3,  4,  5,
  46.      4,  5,  6,  7,  8,  9,
  47.      8,  9, 10, 11, 12, 13,
  48.     12, 13, 14, 15, 16, 17,
  49.     16, 17, 18, 19, 20, 21,
  50.     20, 21, 22, 23, 24, 25,
  51.     24, 25, 26, 27, 28, 29,
  52.     28, 29, 30, 31, 32,  1 
  53. };
  54. #endif
  55.  
  56. /* permuted choice table (key) */
  57. static char pc1[] = {
  58.     57, 49, 41, 33, 25, 17,  9,
  59.      1, 58, 50, 42, 34, 26, 18,
  60.     10,  2, 59, 51, 43, 35, 27,
  61.     19, 11,  3, 60, 52, 44, 36,
  62.  
  63.     63, 55, 47, 39, 31, 23, 15,
  64.      7, 62, 54, 46, 38, 30, 22,
  65.     14,  6, 61, 53, 45, 37, 29,
  66.     21, 13,  5, 28, 20, 12,  4
  67. };
  68.  
  69. /* number left rotations of pc1 */
  70. static char totrot[] = {
  71.     1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28
  72. };
  73.  
  74. /* permuted choice key (table) */
  75. static char pc2[] = {
  76.     14, 17, 11, 24,  1,  5,
  77.      3, 28, 15,  6, 21, 10,
  78.     23, 19, 12,  4, 26,  8,
  79.     16,  7, 27, 20, 13,  2,
  80.     41, 52, 31, 37, 47, 55,
  81.     30, 40, 51, 45, 33, 48,
  82.     44, 49, 39, 56, 34, 53,
  83.     46, 42, 50, 36, 29, 32
  84. };
  85.  
  86. /* The (in)famous S-boxes */
  87. static char si[8][64] = {
  88.     /* S1 */
  89.     14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7,
  90.      0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8,
  91.      4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0,
  92.     15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13,
  93.  
  94.     /* S2 */
  95.     15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10,
  96.      3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5,
  97.      0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15,
  98.     13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9,
  99.  
  100.     /* S3 */
  101.     10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8,
  102.     13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1,
  103.     13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7,
  104.      1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12,
  105.  
  106.     /* S4 */
  107.      7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15,
  108.     13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9,
  109.     10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4,
  110.      3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14,
  111.  
  112.     /* S5 */
  113.      2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9,
  114.     14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6,
  115.      4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14,
  116.     11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3,
  117.  
  118.     /* S6 */
  119.     12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11,
  120.     10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8,
  121.      9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6,
  122.      4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13,
  123.  
  124.     /* S7 */
  125.      4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1,
  126.     13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6,
  127.      1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2,
  128.      6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12,
  129.  
  130.     /* S8 */
  131.     13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7,
  132.      1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2,
  133.      7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8,
  134.      2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11
  135. };
  136.  
  137. /* 32-bit permutation function P used on the output of the S-boxes */
  138. static char p32i[] = {    
  139.     16,  7, 20, 21,
  140.     29, 12, 28, 17,
  141.      1, 15, 23, 26,
  142.      5, 18, 31, 10,
  143.      2,  8, 24, 14,
  144.     32, 27,  3,  9,
  145.     19, 13, 30,  6,
  146.     22, 11,  4, 25
  147. };
  148. /* End of DES-defined tables */
  149.  
  150. /* Lookup tables initialized once only at startup by desinit() */
  151. static long (*sp)[64];        /* Combined S and P boxes */
  152.  
  153. static char (*iperm)[16][8];    /* Initial and final permutations */
  154. static char (*fperm)[16][8];
  155.  
  156. /* 8 6-bit subkeys for each of 16 rounds, initialized by setkey() */
  157. static unsigned char (*kn)[8];
  158.  
  159. /* bit 0 is left-most in byte */
  160. static int bytebit[] = {
  161.     0200,0100,040,020,010,04,02,01
  162. };
  163.  
  164. static int nibblebit[] = {
  165.      010,04,02,01
  166. };
  167. static int desmode;
  168.  
  169. /* Allocate space and initialize DES lookup arrays
  170.  * mode == 0: standard Data Encryption Algorithm
  171.  * mode == 1: DEA without initial and final permutations for speed
  172.  * mode == 2: DEA without permutations and with 128-byte key (completely
  173.  *            independent subkeys for each round)
  174.  */
  175. desinit(mode)
  176. int mode;
  177. {
  178.     char *malloc();
  179.  
  180.     if(sp != NULL){
  181.         /* Already initialized */
  182.         return 0;
  183.     }
  184.     desmode = mode;
  185.     
  186.     if((sp = (long (*)[64])malloc(sizeof(long) * 8 * 64)) == NULL){
  187.         return -1;
  188.     }
  189.     spinit();
  190.     kn = (unsigned char (*)[8])malloc(sizeof(char) * 8 * 16);
  191.     if(kn == NULL){
  192.         free((char *)sp);
  193.         return -1;
  194.     }
  195.     if(mode == 1 || mode == 2)    /* No permutations */
  196.         return 0;
  197.  
  198.     iperm = (char (*)[16][8])malloc(sizeof(char) * 16 * 16 * 8);
  199.     if(iperm == NULL){
  200.         free((char *)sp);
  201.         free((char *)kn);
  202.         return -1;
  203.     }
  204.     perminit(iperm,ip);
  205.  
  206.     fperm = (char (*)[16][8])malloc(sizeof(char) * 16 * 16 * 8);
  207.     if(fperm == NULL){
  208.         free((char *)sp);
  209.         free((char *)kn);
  210.         free((char *)iperm);
  211.         return -1;
  212.     }
  213.     perminit(fperm,fp);
  214.     
  215.     return 0;
  216. }
  217. /* Free up storage used by DES */
  218. desdone()
  219. {
  220.     if(sp == NULL)
  221.         return;    /* Already done */
  222.  
  223.     free((char *)sp);
  224.     free((char *)kn);
  225.     if(iperm != NULL)
  226.         free((char *)iperm);
  227.     if(fperm != NULL)
  228.         free((char *)fperm);
  229.  
  230.     sp = NULL;
  231.     iperm = NULL;
  232.     fperm = NULL;
  233.     kn = NULL;
  234. }
  235. /* Set key (initialize key schedule array) */
  236. setkey(key)
  237. char *key;            /* 64 bits (will use only 56) */
  238. {
  239.     char pc1m[56];        /* place to modify pc1 into */
  240.     char pcr[56];        /* place to rotate pc1 into */
  241.     register int i,j,l;
  242.     int m;
  243.  
  244.     /* In mode 2, the 128 bytes of subkey are set directly from the
  245.      * user's key, allowing him to use completely independent
  246.      * subkeys for each round. Note that the user MUST specify a
  247.      * full 128 bytes.
  248.      *
  249.      * I would like to think that this technique gives the NSA a real
  250.      * headache, but I'm not THAT naive.
  251.      */
  252.     if(desmode == 2){
  253.         for(i=0;i<16;i++)
  254.             for(j=0;j<8;j++)
  255.                 kn[i][j] = *key++;
  256.         return;
  257.     }
  258.     /* Clear key schedule */
  259.     for (i=0; i<16; i++)
  260.         for (j=0; j<8; j++)
  261.             kn[i][j]=0;
  262.  
  263.     for (j=0; j<56; j++) {        /* convert pc1 to bits of key */
  264.         l=pc1[j]-1;        /* integer bit location     */
  265.         m = l & 07;        /* find bit         */
  266.         pc1m[j]=(key[l>>3] &    /* find which key byte l is in */
  267.             bytebit[m])    /* and which bit of that byte */
  268.             ? 1 : 0;    /* and store 1-bit result */
  269.     }
  270.     for (i=0; i<16; i++) {        /* key chunk for each iteration */
  271.         for (j=0; j<56; j++)    /* rotate pc1 the right amount */
  272.             pcr[j] = pc1m[(l=j+totrot[i])<(j<28? 28 : 56) ? l: l-28];
  273.             /* rotate left and right halves independently */
  274.         for (j=0; j<48; j++){    /* select bits individually */
  275.             /* check bit that goes to kn[j] */
  276.             if (pcr[pc2[j]-1]){
  277.                 /* mask it in if it's there */
  278.                 l= j % 6;
  279.                 kn[i][j/6] |= bytebit[l] >> 2;
  280.             }
  281.         }
  282.     }
  283. }
  284. /* In-place encryption of 64-bit block */
  285. endes(block)
  286. char *block;
  287. {
  288.     register int i;
  289.     unsigned long work[2];         /* Working data storage */
  290.     long tmp;
  291.  
  292.     permute(block,iperm,(char *)work);    /* Initial Permutation */
  293. #ifdef LITTLE_ENDIAN
  294.     work[0] = byteswap(work[0]);
  295.     work[1] = byteswap(work[1]);
  296. #endif
  297.  
  298.     /* Do the 16 rounds */
  299.     for (i=0; i<16; i++)
  300.         round(i,work);
  301.  
  302.     /* Left/right half swap */
  303.     tmp = work[0];
  304.     work[0] = work[1];    
  305.     work[1] = tmp;
  306.  
  307. #ifdef LITTLE_ENDIAN
  308.     work[0] = byteswap(work[0]);
  309.     work[1] = byteswap(work[1]);
  310. #endif
  311.     permute((char *)work,fperm,block);    /* Inverse initial permutation */
  312. }
  313. /* In-place decryption of 64-bit block */
  314. dedes(block)
  315. char *block;
  316. {
  317.     register int i;
  318.     unsigned long work[2];    /* Working data storage */
  319.     long tmp;
  320.  
  321.     permute(block,iperm,(char *)work);    /* Initial permutation */
  322.  
  323. #ifdef LITTLE_ENDIAN
  324.     work[0] = byteswap(work[0]);
  325.     work[1] = byteswap(work[1]);
  326. #endif
  327.  
  328.     /* Left/right half swap */
  329.     tmp = work[0];
  330.     work[0] = work[1];    
  331.     work[1] = tmp;
  332.  
  333.     /* Do the 16 rounds in reverse order */
  334.     for (i=15; i >= 0; i--)
  335.         round(i,work);
  336.  
  337. #ifdef LITTLE_ENDIAN
  338.     work[0] = byteswap(work[0]);
  339.     work[1] = byteswap(work[1]);
  340. #endif
  341.  
  342.     permute((char *)work,fperm,block);    /* Inverse initial permutation */
  343. }
  344.  
  345. /* Permute inblock with perm */
  346. static
  347. permute(inblock,perm,outblock)
  348. char *inblock, *outblock;        /* result into outblock,64 bits */
  349. char perm[16][16][8];            /* 2K bytes defining perm. */
  350. {
  351.     register int i,j;
  352.     register char *ib, *ob;        /* ptr to input or output block */
  353.     register char *p, *q;
  354.  
  355.     if(perm == NULL){
  356.         /* No permutation, just copy */
  357.         for(i=8; i!=0; i--)
  358.             *outblock++ = *inblock++;
  359.         return;
  360.     }
  361.     /* Clear output block     */
  362.     for (i=8, ob = outblock; i != 0; i--)
  363.         *ob++ = 0;
  364.  
  365.     ib = inblock;
  366.     for (j = 0; j < 16; j += 2, ib++) { /* for each input nibble */
  367.         ob = outblock;
  368.         p = perm[j][(*ib >> 4) & 017];
  369.         q = perm[j + 1][*ib & 017];
  370.         for (i = 8; i != 0; i--){   /* and each output byte */
  371.             *ob++ |= *p++ | *q++;    /* OR the masks together*/
  372.         }
  373.     }
  374. }
  375.  
  376. /* Do one DES cipher round */
  377. static
  378. round(num,block)
  379. int num;                /* i.e. the num-th one     */
  380. unsigned long *block;
  381. {
  382.     long f();
  383.  
  384.     /* The rounds are numbered from 0 to 15. On even rounds
  385.      * the right half is fed to f() and the result exclusive-ORs
  386.      * the left half; on odd rounds the reverse is done.
  387.      */
  388.     if(num & 1){
  389.         block[1] ^= f(block[0],kn[num]);
  390.     } else {
  391.         block[0] ^= f(block[1],kn[num]);
  392.     }
  393. }
  394. /* The nonlinear function f(r,k), the heart of DES */
  395. static
  396. long
  397. f(r,subkey)
  398. unsigned long r;        /* 32 bits */
  399. unsigned char subkey[8];    /* 48-bit key for this round */
  400. {
  401.     register unsigned long rval,rt;
  402. #ifdef    TRACE
  403.     unsigned char *cp;
  404.     int i;
  405.  
  406.     printf("f(%08lx, %02x %02x %02x %02x %02x %02x %02x %02x) = ",
  407.         r,
  408.         subkey[0], subkey[1], subkey[2],
  409.         subkey[3], subkey[4], subkey[5],
  410.         subkey[6], subkey[7]);
  411. #endif
  412.     /* Run E(R) ^ K through the combined S & P boxes
  413.      * This code takes advantage of a convenient regularity in
  414.      * E, namely that each group of 6 bits in E(R) feeding
  415.      * a single S-box is a contiguous segment of R.
  416.      */
  417.     rt = (r >> 1) | ((r & 1) ? 0x80000000 : 0);
  418.     rval = 0;
  419.     rval |= sp[0][((rt >> 26) ^ *subkey++) & 0x3f];
  420.     rval |= sp[1][((rt >> 22) ^ *subkey++) & 0x3f];
  421.     rval |= sp[2][((rt >> 18) ^ *subkey++) & 0x3f];
  422.     rval |= sp[3][((rt >> 14) ^ *subkey++) & 0x3f];
  423.     rval |= sp[4][((rt >> 10) ^ *subkey++) & 0x3f];
  424.     rval |= sp[5][((rt >> 6) ^ *subkey++) & 0x3f];
  425.     rval |= sp[6][((rt >> 2) ^ *subkey++) & 0x3f];
  426.     rt = (r << 1) | ((r & 0x80000000) ? 1 : 0);
  427.     rval |= sp[7][(rt ^ *subkey) & 0x3f];
  428. #ifdef    TRACE
  429.     printf(" %08lx\n",rval);
  430. #endif
  431.     return rval;
  432. }
  433. /* initialize a perm array */
  434. static
  435. perminit(perm,p)
  436. char perm[16][16][8];            /* 64-bit, either init or final */
  437. char p[64];
  438. {
  439.     register int l, j, k;
  440.     int i,m;
  441.  
  442.     /* Clear the permutation array */
  443.     for (i=0; i<16; i++)
  444.         for (j=0; j<16; j++)
  445.             for (k=0; k<8; k++)
  446.                 perm[i][j][k]=0;
  447.  
  448.     for (i=0; i<16; i++)        /* each input nibble position */
  449.         for (j = 0; j < 16; j++)/* each possible input nibble */
  450.         for (k = 0; k < 64; k++)/* each output bit position */
  451.         {   l = p[k] - 1;    /* where does this bit come from*/
  452.             if ((l >> 2) != i)  /* does it come from input posn?*/
  453.             continue;    /* if not, bit k is 0     */
  454.             if (!(j & nibblebit[l & 3]))
  455.             continue;    /* any such bit in input? */
  456.             m = k & 07;    /* which bit is this in the byte*/
  457.             perm[i][j][k>>3] |= bytebit[m];
  458.         }
  459. }
  460.  
  461. /* Initialize the lookup table for the combined S and P boxes */
  462. static int
  463. spinit()
  464. {
  465.     char pbox[32];
  466.     int p,i,s,j,rowcol;
  467.     long val;
  468.  
  469.     /* Compute pbox, the inverse of p32i.
  470.      * This is easier to work with
  471.      */
  472.     for(p=0;p<32;p++){
  473.         for(i=0;i<32;i++){
  474.             if(p32i[i]-1 == p){
  475.                 pbox[p] = i;
  476.                 break;
  477.             }
  478.         }
  479.     }
  480.     for(s = 0; s < 8; s++){            /* For each S-box */
  481.         for(i=0; i<64; i++){        /* For each possible input */
  482.             val = 0;
  483.             /* The row number is formed from the first and last
  484.              * bits; the column number is from the middle 4
  485.              */
  486.             rowcol = (i & 32) | ((i & 1) ? 16 : 0) | ((i >> 1) & 0xf);
  487.             for(j=0;j<4;j++){    /* For each output bit */
  488.                 if(si[s][rowcol] & (8 >> j)){
  489.                  val |= 1L << (31 - pbox[4*s + j]);
  490.                 }
  491.             }
  492.             sp[s][i] = val;
  493.  
  494. #ifdef    DEBUG
  495.             printf("sp[%d][%2d] = %08lx\n",s,i,sp[s][i]);
  496. #endif
  497.         }
  498.     }
  499. }
  500. #ifdef    LITTLE_ENDIAN
  501. /* Byte swap a long */
  502. static
  503. unsigned long
  504. byteswap(x)
  505. unsigned long x;
  506. {
  507.     register char *cp,tmp;
  508.  
  509.     cp = (char *)&x;
  510.     tmp = cp[3];
  511.     cp[3] = cp[0];
  512.     cp[0] = tmp;
  513.  
  514.     tmp = cp[2];
  515.     cp[2] = cp[1];
  516.     cp[1] = tmp;
  517.  
  518.     return x;
  519. }
  520. #endif
  521.